package com.zhiyi.app.aop;

import com.zhiyi.app.annotation.HasResource;
import com.zhiyi.app.entity.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * 声明当前是一个切面类
 */
@Component
@Aspect
public class CheckResourceAop {
    @Autowired
    private HttpSession session;
    /**
     * 切入点
     * @param resource
     */
    @Pointcut("@annotation(resource) && within(com.zhiyi.app.controller..*)")
    public void cut(HasResource resource){

    }
    @Around("cut(resource)")
    public Object round(ProceedingJoinPoint point, HasResource resource){
//        获取注解中的值
        String r=resource.value();
        User u= (User) session.getAttribute("user");
//        判断用户是否拥有权限
        if (u.getResouces().contains(r)){
            try {
                Object o= point.proceed();
                return o;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        return "403";
    }
}
